﻿using Ewell.BizComponent.Service.Model.Request;
using Ewell.BizComponent.Service.Model.Response;
using Ewell.BizComponent.Service.Server;
using System;
using System.Collections.Generic;
using System.Windows;

namespace Ewell.BizComponent.Charge.UI.VM
{
    public class OnlinePayVm : NotifyPropertyChanged
    {
        /// <summary>
        /// 是否继续
        /// </summary>
        private bool _canEnabled;
        public bool canEnabled
        {
            get
            {
                return _canEnabled;
            }
            set
            {
                if (_canEnabled != value)
                {
                    _canEnabled = value;
                    OnPropertyChanged(nameof(canEnabled));
                }
            }
        }

        public Window localWindows { get; set; }
        /// <summary>
        /// 线上支付的支付方式
        /// </summary>
        private List<PayWayVm> _payWayList;
        public List<PayWayVm> payWayList
        {
            get => _payWayList; set
            {
                if (value != _payWayList)
                {
                    _payWayList = value;
                    OnPropertyChanged(nameof(payWayList));
                }
            }
        }
        /// <summary>
        /// 当前支付中的支付方式
        /// </summary>
        private PayWayVm _payWay;
        public PayWayVm payWay
        {
            get => _payWay;
            set
            {
                if (_payWay != value)
                {
                    _payWay = value;
                    OnPropertyChanged(nameof(payWay));
                }
            }
        }
        /// <summary>
        /// 患者信息
        /// </summary>
        private PatientVm _patientVm;
        public PatientVm patientVm
        {
            get => _patientVm; set
            {
                if (value != _patientVm)
                {
                    _patientVm = value;
                    OnPropertyChanged(nameof(patientVm));
                }
            }
        }
        /// <summary>
        /// 收费信息
        /// </summary>
        private ChargingInfoVm _chargingInfo;
        public ChargingInfoVm chargingInfo
        {
            get => _chargingInfo; set
            {
                if (_chargingInfo != value)
                {
                    _chargingInfo = value;
                    OnPropertyChanged(nameof(chargingInfo));
                }
            }
        }

        /// <summary>
        /// 支付接口支付
        /// </summary>
        /// <returns></returns>
        public PaymentResp Pay(PayWayVm payWay)
        {
            PaymentReq paymentRequest = new PaymentReq()
            {
                patientId = patientVm.patientId,
                hisBizNo = chargingInfo.settlementId.ToString(),
                hisBizTypeCode = chargingInfo.settlementTypeCode,
                payWayNo = payWay.no,
                payAmt = payWay.amount,
                payCode = payWay.payQrCode,
            };

            PaymentResp result = default;
            PaymentServer paymentServer = new PaymentServer();
            try
            {
                result = paymentServer.Pay(payWay.interfaceId.Value, paymentRequest);
            }
            catch (Exception ex)
            {
                payWay.paySuccess = false;
                payWay.payErrorMsg = ex.Message;
                return result;
            }

            payWay.paySuccess = true;
            payWay.payTxnNo = result?.payTxnNo;
            payWay.payAmt = payWay.amount;
            return result;
        }
        /// <summary>
        /// 支付接口退费
        /// </summary>
        /// <returns></returns>
        public RefundResp Refund(PayWayVm payWay)
        {
            canEnabled = false;

            RefundReq refundRequest = new RefundReq()
            {
                patientId = patientVm.patientId,
                refundHisBizNo = chargingInfo.settlementId.ToString(),
                hisBizTypeCode = chargingInfo.settlementTypeCode,
                oldPayTxnNo = payWay.payTxnNo,
                payWayNo = payWay.no,
                refundAmt = payWay.payAmt?.ToString(),
            };

            RefundResp result = default;
            PaymentServer paymentServer = new PaymentServer();
            try
            {
                result = paymentServer.Refund(payWay.interfaceId.Value, refundRequest);
            }
            catch (Exception ex)
            {
                payWay.payErrorMsg = ex.Message;
                return result;
            }
            payWay.paySuccess = false;

            return result;
        }
    }
}
